﻿using System;
using System.IO;
using System.IO.MemoryMappedFiles;

namespace _023_将内存映射文件写入磁盘文件
{
    class Program
    {
        static void Main(string[] args)
        {

            using (MemoryMappedFile 读文件操作=MemoryMappedFile.CreateFromFile("demo.data",FileMode.OpenOrCreate,"demo",100L))
            {
                using (var 数据源=读文件操作.CreateViewStream())
                {
                    using (BinaryWriter 写操作=new BinaryWriter(数据源))
                    {
                        写操作.Write(160);
                        写操作.Write(1.27f);
                        写操作.Write(90000L);
                        写操作.Write(13.165d);
                    }
                }
            }

            //从生成的磁盘文件中依次读出这四个值
            using (FileStream 读文件的流=File.OpenRead("demo.data"))
            {
                Console.WriteLine($"文件的大小为：{读文件的流.Length}");
                using (BinaryReader 读取小伙子=new BinaryReader(读文件的流))
                {
                    int v1 = 读取小伙子.ReadInt32();
                    float v2 = 读取小伙子.ReadSingle();
                    long v3 = 读取小伙子.ReadInt64();
                    double v4 = 读取小伙子.ReadDouble();
                    Console.WriteLine($"读到的Int值：{v1}");
                    Console.WriteLine($"读到的float值：{v2}");
                    Console.WriteLine($"读到的long值：{v3}");
                    Console.WriteLine($"读到的double值：{v4}");
                }
            }


        }
    }
}
